﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Alfa.Common.ValueConverters {
	/// <summary>
	/// This class is XAML Value Converter. It convert any text as <see cref="Visibility.Visible"/>, and Null or WhiteSpace as <see cref="Visibility.Collapsed"/> 
	/// </summary>
	public sealed class StringToVisibilityConverter : IValueConverter {
		/// <summary>
		/// Convert any text as <see cref="Visibility.Visible"/>, and Null or WhiteSpace as <see cref="Visibility.Collapsed"/> 
		/// </summary>
		/// <param name="value">Value to convert. Should be string.</param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns>Converted value (<see cref="Visibility"/>)</returns>
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
			return (value is string && string.IsNullOrWhiteSpace((string)value)) ? Visibility.Collapsed : Visibility.Visible;
		}

		/// <summary>
		/// Convert back <see cref="Visibility"/> to text. Visibility.ToString()
		/// </summary>
		/// <param name="value">Value to convert. Should be <see cref="Visibility"/>.</param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns>Converted value (string)</returns>
		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
			return value is Visibility ? ((Visibility)value).ToString( ) : "";
		}
	}
}
